package Enviroment;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;

import Logic.Game;

public class RectangleItem extends QuadItem
{
	private int x = (int)(Math.random()*Game.ENVIROMENT_WIDTH+1);
	public int X(){return x;}
	private int y = (int)(Math.random()*Game.ENVIROMENT_WIDTH+1);
	public int Y(){return y;}
	private int width = (int)(Math.random()*5+15);
	private int height = (int)(Math.random()*5+15);
	
	private int speedX = (int)(Math.random()*1+1);
	private int speedY =(int)(Math.random()*1+1);

	private Color colorDraw = Color.RED;
	
	public RectangleItem()
	{
		int swap = (int)(Math.random()*100);
		if(swap > 50)
			this.speedX = -this.speedX;
		swap = (int)(Math.random()*100);
		if(swap > 50)
			this.speedY = -this.speedY;
	}
	
	public void move()
	{
		x = (int)(Math.random()*Game.ENVIROMENT_WIDTH+1);
		y = (int)(Math.random()*Game.ENVIROMENT_WIDTH+1);
	}
	
	public RectangleItem(int width, int height)
	{
		this.width = width;
		this.height = height;
		this.x = 500;
		this.y = 500;
	}
	
	@Override
	public Bounds getBounds() {
		return new Bounds(x, y, width, height);
	}
	
	public void render(Graphics2D g2d)
	{
		g2d.setColor(colorDraw);
		g2d.fillRect(x - width/2, y - height/2, width, height);
		g2d.setColor(Color.WHITE);
	}
	
	public String toString()
	{
		return x+" "+y+" "+width+" "+height;
	}
	
	public void hasCollided(QuadItem item)
	{
		if(item instanceof RectangleItem)
			hasCollide((RectangleItem)item);
	}
	
	public void hasCollide(RectangleItem other)
	{
		if(doesItemIntersectNode(x, y, width, height, other.x, other.y, other.width, other.height))
			colorDraw = Color.GREEN;
	}
	
	public void collededWith()
	{
		colorDraw = Color.GREEN;
	}
	
	public static boolean doesItemIntersectNode(int sx, int sy, int sw, int sh, int rx, int ry, int rw, int rh) {
	    Rectangle2D s = new Rectangle2D.Double(sx, sy, sw, sh),
	                r = new Rectangle2D.Double(rx, ry, rw, rh);
	    return s.intersects(r);
	}
	
	
	public void changeColor()
	{
		colorDraw = Color.BLUE;
	}
	
	public void changeColorStuck()
	{
		colorDraw = Color.YELLOW;
	}
	
	public void show()
	{
		colorDraw = Color.WHITE;
	}
	
	public boolean tick()
	{
		colorDraw = Color.RED;
		this.x += speedX;
		this.y += speedY;
		if(this.x > Game.ENVIROMENT_WIDTH)
		{
			this.x = Game.ENVIROMENT_WIDTH;
			this.speedX = -this.speedX;
		}
		else if(this.x < 0)
		{
			this.x = 0;
			this.speedX = -this.speedX;
		}
		if(this.y > Game.ENVIROMENT_WIDTH)
		{
			this.y = Game.ENVIROMENT_WIDTH;
			this.speedY = -this.speedY;
		}
		else if(this.y < 0)
		{
			this.y = 0;
			this.speedY = -this.speedY;
		}
		return false;
	}
}